package cn.feng.thinkInJava.a9_2_泛型.generics.a15_04_05;

//: generics/BankTeller.java
// A very simple bank teller simulation.
import java.util.*;
import net.mindview.util.*;

/**泛型应用于内部类及匿名内部类
 * @author fengyu
 * @date  2015年8月9日
 */
class Customer
{
	private static long counter=1;
	private final long id=counter++;
	//构造器私有
	private Customer()
	{
	}
	
	public String toString()
	{
		return "Customer "+id;
	}
	
	// A method to produce Generator objects:匿名内部类实现了Generator接口
	/**
	 * 强制使用Generator对象
	 *@author fengyu
	 * @date  2015年8月12日
	 * @return
	 */
	public static Generator<Customer> generator()
	{
		return new Generator<Customer>()
		{
			public Customer next()
			{
				return new Customer();
			}
		};
	}
}

class Teller
{
	private static long counter=1;
	private final long id=counter++;
	//构造器私有
	private Teller()
	{
	}
	
	public String toString()
	{
		return "Teller "+id;
	}
	// A single Generator object:
	public static Generator<Teller> generator=new Generator<Teller>()
	{
		public Teller next()
		{
			return new Teller();
		}
	};
}

public class BankTeller
{
	public static void serve(Teller t,Customer c)
	{
		System.out.println(t+" serves "+c);
	}
	
	public static void main(String[] args)
	{
		Random rand=new Random(47);
		Queue<Customer> line=new LinkedList<Customer>();
		Generators.fill(line,Customer.generator(),15);
		List<Teller> tellers=new ArrayList<Teller>();
		Generators.fill(tellers,Teller.generator,4);
		for(Customer c:line)
			serve(tellers.get(rand.nextInt(tellers.size())),c);
	}
} /* Output:
Teller 3 serves Customer 1
Teller 2 serves Customer 2
Teller 3 serves Customer 3
Teller 1 serves Customer 4
Teller 1 serves Customer 5
Teller 3 serves Customer 6
Teller 1 serves Customer 7
Teller 2 serves Customer 8
Teller 3 serves Customer 9
Teller 3 serves Customer 10
Teller 2 serves Customer 11
Teller 4 serves Customer 12
Teller 2 serves Customer 13
Teller 1 serves Customer 14
Teller 1 serves Customer 15
*///:~
